package com.z100h.supplychain.evidence.web;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.rribbit.RequestResponseBus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.RRBHints;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.z100h.supplychain.core.EvidenceType;
import com.z100h.supplychain.core.ProductionState;
import com.z100h.supplychain.customer.entity.CustomerStockroom;
import com.z100h.supplychain.evidence.entity.Stockout;
import com.z100h.supplychain.evidence.entity.StockoutErpResourceVo;
import com.z100h.supplychain.evidence.entity.StockoutSku;
import com.z100h.supplychain.evidence.entity.StockoutSkuVo;
import com.z100h.supplychain.evidence.facade.StockoutFacade;
import com.z100h.supplychain.sku.entity.Sku;
import com.z100h.supplychain.sku.service.SkuService;

/**
 * 出库单Controller
 *
 * @author madara
 * @version 2019-01-17
 */
@Controller
@RequestMapping(value = "${adminPath}/sc/stockout")
public class StockoutController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(StockoutController.class);

	@Autowired
	private StockoutFacade<Stockout> stockoutFacade;

	@Autowired
	private SkuService skuService;

	@Autowired
	private RequestResponseBus rrb;

	// @Autowired
	// private StockoutService stockoutService;
	//
	// @Autowired
	// private StockoutSkuService stockoutSkuService;

	/**
	 * 获取出库字典值
	 * @param page
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "outType")
	public Map<String, Object> outType(Page<Stockout> page, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<>();
		map.put("outType", EvidenceType.getOutType());
		return map;
	}

	@ResponseBody
	@RequestMapping(value = "stockoutType")
	public Map<String, Object> stockoutType(Page<Stockout> page, HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> map = new HashMap<>();
		map.put("outType", EvidenceType.getStockoutType());
		return map;
	}

	/**
	 * 
	 * 租赁出库类型
	 * 
	 * @param page
	 * @param request
	 * @param response
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年4月15日 下午3:10:13
	 */
	@ResponseBody
	@RequestMapping(value = "rentOutType")
	public Map<String, Object> rentOutType(Page<Stockout> page, HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> map = new HashMap<>();
		map.put("outType", EvidenceType.getRentOutType());
		return map;
	}

	/**
	 * 
	 * 复壮调换出库类型
	 * 
	 * @param page
	 * @param request
	 * @param response
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年4月22日 下午3:09:16
	 */
	@ResponseBody
	@RequestMapping(value = "repairOutType")
	public Map<String, Object> repairOutType(Page<Stockout> page, HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> map = new HashMap<>();
		map.put("outType", EvidenceType.getRepairOutType());
		return map;
	}

	/**
	 * 出库单列表页面
	 *
	 * @return
	 */
	@RequiresPermissions("sc:stockout:list")
	@RequestMapping(value = { "list", "" })
	public String list() {
		return "supplychain/stockout/stockoutList";
	}

	@RequestMapping(value = "qcList")
	public String qcList() {
		return "supplychain/stockout/qcList";
	}

	@RequestMapping(value = "outList")
	public String outList() {
		return "supplychain/stockout/outList";
	}

	/**
	 * 列表数据页面
	 *
	 * @param page
	 * @param stockout
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequiresPermissions("sc:stockout:data")
	@RequestMapping(value = "data")
	public Map<String, Object> data(Page<Stockout> page, Stockout stockout, HttpServletRequest request,
			HttpServletResponse response) {
		stockout.setCompanyId(UserUtils.getUser().getCompanyId());
		page = stockoutFacade.findStockoutByPage(page, stockout);
		return getBootstrapData(page);
	}

	/**
	 * 跳转新增出库单页面
	 *
	 * @param request
	 * @return
	 */
	@RequiresPermissions("sc:stockout:add")
	@RequestMapping(value = "addStockout")
	public String addStockout(HttpServletRequest request) {

		// 查询同仓库下的sku
		Sku sku = new Sku();
		sku.setDepartmentId(UserUtils.getUser().getOffice().getId());
		List<Sku> skuList = skuService.findList(sku);
		request.setAttribute("skuList", skuList);

		return "supplychain/stockout/add_stockout";
	}

	/**
	 * 
	 * 租赁出库页面
	 * 
	 * @param request
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年4月15日 下午2:56:17
	 */
	@RequestMapping(value = "addRentStockout")
	public String addRentStockout(HttpServletRequest request) {
		return "supplychain/stockout/addRentStockout";
	}

	/**
	 * 
	 * 复壮出库页面
	 * 
	 * @param request
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年4月22日 上午11:37:26
	 */
	@RequestMapping(value = "addRepairStockout")
	public String addRepairStockout(HttpServletRequest request) {
		return "supplychain/stockout/addRepairStockout";
	}

	/**
	 * 保存或修改出库单
	 *
	 * @param request
	 * @param stockout
	 * @return
	 */
	@ResponseBody
	@RequiresPermissions("sc:stockout:save")
	@RequestMapping(value = "save")
	public Map<String, Object> save(HttpServletRequest request, Stockout stockout, String jsons, String orderIds,
			String stockroomId) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			JSONArray skuList = JSONArray.parseArray(StringEscapeUtils.unescapeHtml4(jsons));
			List<String> list = null;
			if (StringUtils.isNotBlank(orderIds)) {
				String[] strs = orderIds.split(",");
				list = Arrays.asList(strs);
			}
			List<String> evidenceIds = stockoutFacade.save(stockout, skuList, list, stockroomId);
			if (CollectionUtils.isNotEmpty(evidenceIds)) {
				for (String evidenceId : evidenceIds) {
					// 记录采购确认流程
					rrb.send(RRBHints.PRODUCTION_PROC.PURCHASE_CONFIRM, evidenceId, null);
				}
			}
			map.put("message", "0");
		} catch (Exception e) {
			map.put("message", "1");
			map.put("data", e.getMessage());
			logger.error("Controller Error StockoutController -> save   " + e.getMessage());
		}
		return map;
	}

	/**
	 * 
	 * 微信生成生产单
	 * 
	 * @param request
	 * @param evidenceId
	 * @return
	 * @throws Throwable
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2020年4月24日 下午2:21:09
	 */
	@ResponseBody
	@RequestMapping(value = "mp/save")
	public AjaxJson mpSave(HttpServletRequest request, String stockroomId) throws Throwable {
		AjaxJson respBody = new AjaxJson();
		try {
			if (localLogin(request)) {

				List<String> evidenceIds = stockoutFacade.save(stockroomId);

				if (CollectionUtils.isNotEmpty(evidenceIds)) {
					for (String evidenceId : evidenceIds) {
						// 记录采购确认流程
						rrb.send(RRBHints.PRODUCTION_PROC.PURCHASE_CONFIRM, evidenceId, null);
						rrb.send(RRBHints.CUSTOMER_SERVICE.CONTRACT_CONFIRM, evidenceId);

						/**
						 * 写日报
						 */
						Stockout stockout = stockoutFacade.findById(evidenceId);
						if (stockout != null && stockout.getCreateBy() != null
								&& StringUtils.isNotBlank(stockout.getCreateBy().getNo())) {
							rrb.send(RRBHints.ACCOUNT.SAVE_WORK_REPORT, stockout.getCreateBy().getNo(),
									StringUtils.join("[拉单] ", stockout.getStockroomName(),
											" " + DateUtils.formatDateTime(stockout.getCreateDate())));
						}
					}
				}
			}
		} catch (Throwable e) {
			respBody.setSuccess(false);
			logger.error("mpSave is error.", e);
			if (e instanceof DuplicateKeyException) {
				respBody.setMsg("订单不允许重复生产！");
			} else {
				respBody.setMsg("生产单完成异常：" + e.getMessage());
			}
		}
		return respBody;
	}

	/**
	 * 扫码过闸机
	 *
	 * @param request
	 * @param stockout
	 * @return
	 */
	@ResponseBody
	@RequiresPermissions("sc:stockout:passingMachine")
	@RequestMapping(value = "passingMachine")
	public Map<String, Object> passingMachine(HttpServletRequest request, Stockout stockout,
			@RequestParam(value = "ids", required = false) List<String> ids) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			stockout.setResourceIds(ids);
			stockoutFacade.passingMachine(stockout);
			map.put("message", "0");
		} catch (Exception e) {
			map.put("message", "1");
			map.put("data", e.getMessage());// 把异常信息带到前台
			logger.error("Controller Error StockoutController -> passingMachine   " + e.getMessage());
		}
		return map;
	}

	/**
	 * 扫码过闸机跳转
	 *
	 * @param request
	 * @param stockout
	 * @return
	 */
	@RequiresPermissions("sc:stockout:to:passingMachine")
	@RequestMapping(value = "passingMachineView")
	public String passingMachineView(HttpServletRequest request, Stockout stockout) {
		request.setAttribute("evidenceId", stockout.getEvidenceId());
		return "supplychain/stockout/passingMachineView";
	}

	/**
	 * 出库单详情
	 *
	 * @param request
	 * @param stockout
	 * @return
	 */
	@RequiresPermissions("sc:stockout:detail")
	@RequestMapping(value = "detail")
	public String detail(HttpServletRequest request, Stockout stockout) {
		StockoutSku stockoutSku = new StockoutSku();
		stockoutSku.setEvidenceId(stockout.getEvidenceId());
		stockoutSku.setId(stockout.getEvidenceId());
		List<StockoutSku> list = stockoutFacade.findStockoutSkuList(stockoutSku);
		request.setAttribute("list", list);
		request.setAttribute("evidenceId", stockout.getEvidenceId());
		request.setAttribute("sto", stockoutFacade.getStockout(stockout));
		// 查询同仓库下的sku
		Sku sku = new Sku();
		// sku.setDepartmentId(UserUtils.getUser().getOffice().getId());
		List<Sku> skuList = skuService.findList(sku);
		request.setAttribute("skuList", skuList);

		return "supplychain/stockout/view";
	}

	@RequestMapping(value = "test")
	public String test(HttpServletRequest request, Stockout stockout) {

		return "supplychain/test/test";
	}

	@RequestMapping(value = { "stockoutList", "" })
	public String list(HttpServletRequest request, Stockout stockout) {
		Stockout stockout1 = stockoutFacade.get(stockout.getEvidenceId());
		List<StockoutSku> skuList = stockoutFacade.findStockoutSkuByEvidenceId(stockout.getEvidenceId());

		request.setAttribute("stk", stockout1);
		request.setAttribute("skuList", skuList);
		return "supplychain/table/stockoutTable";
	}

	@RequestMapping(value = { "stockoutErpRes", "" })
	public String stockoutErpRes(HttpServletRequest request, String evidenceId, String ids) {
		String[] arr = ids.split(",");
		List<String> list = Arrays.asList(arr);
		Stockout stockout1 = stockoutFacade.get(evidenceId);
		List<StockoutSku> skuList = stockoutFacade.findStockoutSkuErpResByIds(list);

		request.setAttribute("stk", stockout1);
		request.setAttribute("skuList", skuList);
		return "supplychain/table/stockoutTable";
	}

	/**
	 * 
	 *根据订单id查询需要出库的sku
	 * 
	 * @param request
	 * @param stockout
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年4月15日 下午5:29:58
	 */
	@ResponseBody
	@RequestMapping(value = "findStockoutSkuByOrderIds")
	public List<StockoutSku> findStockoutSkuByOrderIds(HttpServletRequest request, String orderIds) {
		String[] strs = orderIds.split(",");
		List<String> list = Arrays.asList(strs);
		return stockoutFacade.findStockoutSkuByOrderIds(list);
	}

	/**
	 * 
	 * 根据仓库id查找未出库的复壮单
	 * 
	 * @param request
	 * @param stockroomId
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年4月22日 下午3:27:55
	 */
	@ResponseBody
	@RequestMapping(value = "findStockoutSkuByStockroomId")
	public List<StockoutSku> findStockoutSkuByStockroomId(HttpServletRequest request, String stockroomId) {
		return stockoutFacade.findStockoutSkuByStockroomId(stockroomId);
	}

	/**
	 * 
	 * 所有未出库的sku
	 * 
	 * @param request
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年8月30日 下午2:44:42
	 */
	@ResponseBody
	@RequestMapping(value = "findSkuNotStockout")
	public List<StockoutSkuVo> findSkuNotStockout(HttpServletRequest request) {
		return stockoutFacade.findSkuNotStockout(UserUtils.getUser().getCompanyId());
	}

	/**
	 * 
	 * 跳转生产单的erp资源界面
	 * 
	 * @param request
	 * @param evidenceId
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年9月10日 上午9:36:39
	 */
	@RequestMapping(value = "findStockoutErpResourceVoList")
	public String findStockoutErpResourceVoList(HttpServletRequest request, String evidenceId) {
		// List<StockoutErpResourceVo> list = stockoutFacade.findStockoutErpResourceVoListByEvidenceId(evidenceId);
		// request.setAttribute("list", list);
		request.setAttribute("evidenceId", evidenceId);
		return "supplychain/stockout/erp_resource";
	}

	/**
	 * 
	 * 获取生产单的erp资源
	 * 
	 * @param request
	 * @param response
	 * @param evidenceId
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年9月19日 下午3:09:36
	 */
	@RequestMapping(value = "findResList")
	@ResponseBody
	public Map<String, Object> findResList(HttpServletRequest request, HttpServletResponse response, String evidenceId,
			@RequestParam(value = "state", required = false) String state) {
		Page<StockoutErpResourceVo> page = new Page<StockoutErpResourceVo>(request, response);
		List<StockoutErpResourceVo> list = stockoutFacade.findStockoutErpResourceVoListByEvidenceId(evidenceId, state);
		page.setList(list);
		return getBootstrapData(page);
	}

	/**
	 * 
	 * 按选择完成生产
	 * 
	 * @param ids
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年9月10日 上午9:35:52
	 */
	@ResponseBody
	@RequestMapping(value = "completeProduction")
	public String completeProduction(String ids) {
		try {
			String[] arr = ids.split(",");
			List<String> list = Arrays.asList(arr);
			stockoutFacade.completeProduction(list);
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
	}

	/**
	 * 
	 * 生产单全量生产完成
	 * 
	 * @param evidenceId
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年9月10日 上午9:37:48
	 */
	@ResponseBody
	@RequestMapping(value = "completeProductionAll")
	public String completeProductionAll(String evidenceId) {
		try {
			stockoutFacade.completeProduction(evidenceId);
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
	}

	/**
	 * 
	 * 质检单列表
	 * 
	 * @param page
	 * @param stockout
	 * @param request
	 * @param response
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年9月10日 下午3:20:25
	 */
	@ResponseBody
	@RequestMapping(value = "listData")
	public Map<String, Object> listData(Page<Stockout> page, Stockout stockout, HttpServletRequest request,
			HttpServletResponse response) {
		page = stockoutFacade.findQcByPage(page, stockout);
		return getBootstrapData(page);
	}

	/**
	 * 
	 * 质检单erp资源
	 * 
	 * @param request
	 * @param evidenceId
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年9月10日 下午3:38:30
	 */
	@RequestMapping(value = "findQcErpResourceVoList")
	public String findQcErpResourceVoList(HttpServletRequest request, String evidenceId) {
		// List<StockoutErpResourceVo> list = stockoutFacade.findQcErpResourceVoListByEvidenceId(evidenceId);
		// request.setAttribute("list", list);
		request.setAttribute("evidenceId", evidenceId);
		return "supplychain/stockout/qcErp_resource";
	}

	@RequestMapping(value = "findQcResList")
	@ResponseBody
	public Map<String, Object> findQcResList(HttpServletRequest request, HttpServletResponse response,
			String evidenceId) {
		Page<StockoutErpResourceVo> page = new Page<StockoutErpResourceVo>(request, response);
		List<StockoutErpResourceVo> list = stockoutFacade.findQcErpResourceVoListByEvidenceId(evidenceId);
		page.setList(list);
		return getBootstrapData(page);
	}

	/**
	 * 
	 * 全量质检
	 * 
	 * @author: Fyh - 1009173473@qq.com 
	 * @throws Throwable 
	 * @date: Created on 2019年9月10日 下午3:48:04
	 */
	@ResponseBody
	@RequestMapping(value = "qualityControl")
	public String qualityControl(String evidenceId, String state) throws Throwable {
		ProductionState productionState = null;
		try {
			if ("0".equals(state)) {
				productionState = ProductionState.QCPASS;
			} else {
				productionState = ProductionState.QCFAILED;
			}
			stockoutFacade.qualityControl(evidenceId, productionState);
			// 记录质检通过记录
			rrb.send(RRBHints.PRODUCTION_PROC.BASE_PRODUCE, evidenceId, null);

			/**
			 * 质检-出库 
			 */
			stockoutFacade.outRes(evidenceId);
			// 记录出库流程
			rrb.send(RRBHints.PRODUCTION_PROC.BASE_OUT, evidenceId, null);

			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}

	}

	/**
	 * 
	 * 全量质检
	 * 
	 * @author: Fyh - 1009173473@qq.com 
	 * @throws Throwable 
	 * @date: Created on 2019年9月10日 下午3:48:04
	 */
	@ResponseBody
	@RequestMapping(value = "mp/qc")
	public AjaxJson qualityControlFromMp(HttpServletRequest request, String evidenceId) throws Throwable {

		AjaxJson respBody = new AjaxJson();
		try {
			if (localLogin(request)) {
				stockoutFacade.qualityControl(evidenceId, ProductionState.QCPASS);

				// 记录质检通过记录
				rrb.send(RRBHints.PRODUCTION_PROC.BASE_PRODUCE, evidenceId, null);
				stockoutFacade.outRes(evidenceId);
				// 记录出库流程
				rrb.send(RRBHints.PRODUCTION_PROC.BASE_OUT, evidenceId, null);
				// 保存业务id扩展属性
				rrb.send(RRBHints.PRODUCTION_PROC.SERVICE_ID_EXTEND, evidenceId);
				// 保存子合同扩展属性
				rrb.send(RRBHints.PRODUCTION_PROC.CONTRACT_EXTEND, evidenceId);

				System.out.println("###################");

				/**
				 * 写日报
				 */
				Stockout stockout = stockoutFacade.findById(evidenceId);
				if (stockout != null && stockout.getCreateBy() != null
						&& StringUtils.isNotBlank(stockout.getCreateBy().getNo())) {
					rrb.send(RRBHints.ACCOUNT.SAVE_WORK_REPORT, stockout.getCreateBy().getNo(),
							StringUtils.join("[质检] ", stockout.getStockroomName(),
									" " + DateUtils.formatDateTime(stockout.getCreateDate()), "拉单 ",
									DateUtils.formatDateTime(stockout.getQcDate()) + "质检"));
				}

				logger.info("sc-act******start:eid={}", evidenceId);
				logger.info("sc-act******start:erpUid={}", UserUtils.getUser().getNo());
				/**
				 * 订单走流程
				 */
				rrb.send(RRBHints.SUPPLY_CHAIN_PROC.ORDER_CLAIM, evidenceId, UserUtils.getUser().getNo());
				logger.info("sc-act******end:eid={}", evidenceId);
				logger.info("sc-act******end:erpUid={}", UserUtils.getUser().getNo());
			}
		} catch (Throwable e) {
			e.printStackTrace();
			respBody.setSuccess(false);
			respBody.setMsg("质检异常：" + e.getMessage());
			logger.error("saveAndQcFromMp is error.", e);
		}
		return respBody;
	}

	@ResponseBody
	@RequestMapping(value = "aaa")
	public String aaa(HttpServletRequest request, String evidenceId) throws Throwable {
		try {
			if (localLogin(request)) {
				logger.info("STEP 4: 开始记录出库流程");
				rrb.send(RRBHints.PRODUCTION_PROC.BASE_OUT, evidenceId, null);

				logger.info("STEP 5: 保存业务id扩展属性");
				rrb.send(RRBHints.PRODUCTION_PROC.SERVICE_ID_EXTEND, evidenceId);

				logger.info("STEP 6: 保存子合同扩展属性");
				rrb.send(RRBHints.PRODUCTION_PROC.CONTRACT_EXTEND, evidenceId);

				logger.info("STEP 7: 出库流程调用完成");
				/**
				 * 订单走流程
				 */
				rrb.send(RRBHints.SUPPLY_CHAIN_PROC.ORDER_CLAIM, evidenceId, UserUtils.getUser().getNo());
			}
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}

	@ResponseBody
	@RequestMapping(value = "bbb")
	public String bbb() throws Throwable {
		try {
			rrb.send(RRBHints.SUPPLY_CHAIN_PROC.ORDER_CLAIM, "21296", "1000001802");
			rrb.send(RRBHints.SUPPLY_CHAIN_PROC.ORDER_CLAIM, "21297", "1000001802");

			rrb.send(RRBHints.SUPPLY_CHAIN_PROC.ORDER_CLAIM, "21131", "1000001831");
			rrb.send(RRBHints.SUPPLY_CHAIN_PROC.ORDER_CLAIM, "21158", "1000001831");
			rrb.send(RRBHints.SUPPLY_CHAIN_PROC.ORDER_CLAIM, "21178", "1000001831");
			rrb.send(RRBHints.SUPPLY_CHAIN_PROC.ORDER_CLAIM, "21246", "1000001831");
			rrb.send(RRBHints.SUPPLY_CHAIN_PROC.ORDER_CLAIM, "21249", "1000001831");
			rrb.send(RRBHints.SUPPLY_CHAIN_PROC.ORDER_CLAIM, "21315", "1000001831");
			rrb.send(RRBHints.SUPPLY_CHAIN_PROC.ORDER_CLAIM, "21318", "1000001831");
			rrb.send(RRBHints.SUPPLY_CHAIN_PROC.ORDER_CLAIM, "21319", "1000001831");

			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}

	/**
	 * 
	 * 逐条质检
	 * 
	 * @param evidenceId
	 * @param ids
	 * @param state
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @throws Throwable 
	 * @date: Created on 2019年9月10日 下午4:14:15
	 */
	@ResponseBody
	@RequestMapping(value = "qualityControlByIds")
	public String qualityControlByIds(String evidenceId, String ids, String state) throws Throwable {
		ProductionState productionState = null;
		try {
			String[] arr = ids.split(",");
			List<String> list = Arrays.asList(arr);
			if ("0".equals(state)) {
				productionState = ProductionState.QCPASS;
			} else {
				productionState = ProductionState.QCFAILED;
			}
			stockoutFacade.qualityControl(evidenceId, list, productionState);
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}

	/**
	 * 
	 * 出库单erp 资源列表
	 * 
	 * @param page
	 * @param stockout
	 * @param request
	 * @param response
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年9月11日 上午10:21:51
	 */
	@ResponseBody
	@RequestMapping(value = "outData")
	public Map<String, Object> outData(Page<Stockout> page, Stockout stockout, HttpServletRequest request,
			HttpServletResponse response) {
		page = stockoutFacade.findOutByPage(page, stockout);
		return getBootstrapData(page);
	}

	/**
	 * 
	 * 出库单erp资源页面
	 * 
	 * @param request
	 * @param evidenceId
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年9月16日 下午2:35:40
	 */
	@RequestMapping(value = "findOutErpResourceVoList")
	public String findOutErpResourceVoList(HttpServletRequest request, String evidenceId) {
		// List<StockoutErpResourceVo> list = stockoutFacade.findOutErpResourceVoListByEvidenceId(evidenceId);
		// request.setAttribute("list", list);
		request.setAttribute("evidenceId", evidenceId);
		return "supplychain/stockout/outErp_resource";
	}

	/**
	 * 
	 * 出库单erp资源
	 * 
	 * @param request
	 * @param response
	 * @param evidenceId
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年9月19日 下午6:11:14
	 */
	@RequestMapping(value = "findOutResList")
	@ResponseBody
	public Map<String, Object> findOutResList(HttpServletRequest request, HttpServletResponse response,
			String evidenceId) {
		Page<StockoutErpResourceVo> page = new Page<StockoutErpResourceVo>(request, response);
		List<StockoutErpResourceVo> list = stockoutFacade.findOutErpResourceVoListByEvidenceId(evidenceId);
		page.setList(list);
		return getBootstrapData(page);
	}

	/**
	 * 
	 * 全量出库
	 * 
	 * @param evidenceId
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年9月16日 下午3:33:54
	 */
	@ResponseBody
	@RequestMapping(value = "ourRes")
	public String ourRes(String evidenceId) {
		try {
			stockoutFacade.outRes(evidenceId);
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
	}

	/**
	 * 
	 * 批量出库
	 * 
	 * @param ids
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年9月16日 下午3:34:08
	 */
	@ResponseBody
	@RequestMapping(value = "ourResByIds")
	public String ourResByIds(String evidenceId, String ids) {
		try {
			String[] arr = ids.split(",");
			List<String> list = Arrays.asList(arr);
			stockoutFacade.outRes(evidenceId, list);
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
	}

	@ResponseBody
	@RequestMapping(value = "getProductionState")
	public JSONArray getProductionState() {
		return ProductionState.getProductionState();
	}

	/**
	 * 
	 * 待生产列表
	 * 
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年9月23日 上午9:33:05
	 */
	@RequestMapping(value = { "needProduceList", "" })
	public String needProduceList() {
		return "supplychain/stockout/needProduceList";
	}

	@ResponseBody
	@RequestMapping(value = "findNeedProduceByPage")
	public Map<String, Object> findNeedProduceByPage(Page<CustomerStockroom> page, CustomerStockroom customerStockroom,
			HttpServletRequest request, HttpServletResponse response) {
		page = stockoutFacade.findNeedProduceByPage(page, customerStockroom);
		return getBootstrapData(page);
	}

	/**
	 * 
	 * 复壮撤销
	 * 
	 * @param stockroomId
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年9月24日 上午11:32:04
	 */
	@ResponseBody
	@RequestMapping(value = "cancelRepair")
	public String cancelRepair(String stockroomId) {
		try {
			// 记录采购拒绝记录
			rrb.send(RRBHints.PRODUCTION_PROC.PURCHASE_REFUSE, stockroomId, null);
			stockoutFacade.cancelRepair(stockroomId);
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
	}

	/**
	 * 
	 * 微信复壮撤销
	 * 
	 * @param request
	 * @param evidenceId
	 * @return
	 * @throws Throwable
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2020年4月28日 下午4:05:12
	 */
	@ResponseBody
	@RequestMapping(value = "mp/cancelRepair")
	public AjaxJson mpCancelRepair(HttpServletRequest request, String stockroomId) throws Throwable {
		AjaxJson respBody = new AjaxJson();
		try {
			if (localLogin(request)) {
				// 记录采购拒绝记录
				rrb.send(RRBHints.PRODUCTION_PROC.PURCHASE_REFUSE, stockroomId, null);
				stockoutFacade.cancelRepair(stockroomId);

			}
		} catch (Throwable e) {
			respBody.setSuccess(false);
			respBody.setMsg(e.getMessage());
			logger.error("mpCancelRepair is error.", e);
		}
		return respBody;
	}

	@ResponseBody
	@RequestMapping(value = "outCommonStockout")
	public String outCommonStockout(String evidenceId) {
		try {
			stockoutFacade.outCommonStockout(evidenceId);
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}

}
